﻿using Pay.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pay.DTO;
using Pay.TO;
using Pay.Token.Utils;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using Flurl.Http;
using Pay.Core;

namespace Pay.Token.Controllers
{
    /// <summary>
    /// 商城分类
    /// </summary>
    [AppAuthorize("Member")]
    public class CategoryController : GeneralController {
        private ICategoryService _iCategoryService;
        private IProductService _iProductService;
        private IMerchantService _iMerchantService;
        private IRedRecordService _iRedRecordService;
        public CategoryController(ICategoryService iCategoryService, IProductService iProductService, IMerchantService iMerchantService, IRedRecordService iRedRecordService) {
            _iCategoryService = iCategoryService;
            _iProductService = iProductService;
            _iMerchantService = iMerchantService;
            _iRedRecordService = iRedRecordService;
        }

        /// <summary>
        /// 分类列表
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult CategoryList() {
            var categoryList = _iCategoryService.GetTokenFirstDepthCatagory();//商城一级分类列表
            ViewBag.CategoryList = categoryList;
            ViewBag.img_url = RequestMethod.token_imgurl;
            return View();
        }
        /// <summary>
        /// 商品分类
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous,HttpGet]
        public JsonResult GetCategorys() {
            var merchant = _iMerchantService.Get(t_RequestMethod.merchant_no);
            var list = _iCategoryService.GetAll(merchant.id, 1);
            return Json(ResultToJson.Json(list), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 分类产品列表
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult CategoryProduct(long categoryId) {
            var category = _iCategoryService.GetTokenCategory(categoryId);
            if(category == null)
                return Redirect("/Category/CategoryList");
            ViewBag.Category = category;
            merchant_dto merchant = new merchant_dto();
            red_user_dto redcord = new red_user_dto();
            if (UserId > 0) {
                merchant = _iMerchantService.Get(UserId);
                redcord = _iRedRecordService.GetUser(UserId);
            }
            ViewBag.merchant = merchant;
            ViewBag.redcord = redcord;
            return View();
        }

        /// <summary>
        /// 商品列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [AllowAnonymous]
        public PartialViewResult _CategoryProduct(GoodsQuery query) {
            GoodsQuery good_query = new GoodsQuery();
            good_query.page = query.page;
            good_query.size = query.size;
            good_query.sale_status = 0;
            good_query.sell_out = false;
            good_query.IsTokenMall = true;
            good_query.keyword = query.keyword;
            good_query.category_id = query.category_id;
            good_query.sort = query.sort;
            var merchant = _iMerchantService.Get(t_RequestMethod.merchant_no);
            good_query.merchant_id = merchant.id;
            var list = _iProductService.GetForActicity(good_query);
            ViewBag.img_url = RequestMethod.token_imgurl;
            int userType = 0;
            if (UserId > 0) {
                var curMerchant = _iMerchantService.Get(UserId);
                userType = (int)curMerchant.usertype;
            }
            ViewBag.UserType = userType;
            return PartialView(list);
        }
    }
}